package com.java.netty.nio.netty.protocoltcp;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ReplayingDecoder;

import java.util.List;

/**
 * @Author: Dgg
 * @Date: 2022/1/25 22:00
 */
public class MyMessageProtoColDecoder extends ReplayingDecoder<Void> {
	@Override
	protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception {
		System.out.println("MyMessageProtoColDecoder 被调用");
		//将得到的二进制字节码转成 ->MessageProtocol 数据包（被调用）
		int length = byteBuf.readInt();
		byte[] context = new byte[length];
		byteBuf.readBytes(context);
		//封装成MessageProtoCol 对象 ，放入 out ，传递下一个handler业务处理
		MessageProtoCol messageProtoCol = new MessageProtoCol();
		messageProtoCol.setLen(length);
		messageProtoCol.setBytes(context);
		list.add(messageProtoCol);
	}
}
